package com.levy.contronller.javaWeb.findImageTest;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;

public class ImageResizer {

    /**
     * 功能：获取图片像素
     * * @param filePath 图片路径
     */
    public static void getPixel(String filePath) {
        File file = new File(filePath);
        BufferedImage bi = null;
        try {
            bi = ImageIO.read(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int width = bi.getWidth(); // 像素
        int height = bi.getHeight(); // 像素
        System.out.println("width=" + width + ",height=" + height + ".");
    }

    /**
     * @param srcPath   源图片路径
     * @param desPath   修改大小后图片路径
     * @param scaleSize 图片的修改比例，目标宽度
     */
    public static void resizeImage(String srcPath, String desPath, int scaleSize) throws IOException {

        File srcFile = new File(srcPath);
        Image srcImg = ImageIO.read(srcFile);
        BufferedImage bi = null;
        try {
            bi = ImageIO.read(srcFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        float width = bi.getWidth(); // 像素
        float height = bi.getHeight(); // 像素
        float scale = width / scaleSize;
        BufferedImage buffImg = null;
        buffImg = new BufferedImage(scaleSize, (int) (height / scale), BufferedImage.TYPE_INT_RGB);
        //使用TYPE_INT_RGB修改的图片会变色
        buffImg.getGraphics().drawImage(
                srcImg.getScaledInstance(scaleSize, (int) (height / scale), Image.SCALE_SMOOTH), 0,
                0, null);

        ImageIO.write(buffImg, "PNG", new File(desPath));
    }

    /**
     * @param srcPath 原图片路径
     * @param desPath 转换大小后图片路径
     * @param width   转换后图片宽度
     * @param height  转换后图片高度
     */
    public static void resizeImage(String srcPath, String desPath,
                                   int width, int height) throws IOException {

        File srcFile = new File(srcPath);
        Image srcImg = ImageIO.read(srcFile);
        BufferedImage buffImg = null;
        buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        //使用TYPE_INT_RGB修改的图片会变色
        buffImg.getGraphics().drawImage(
                srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0,
                0, null);

        ImageIO.write(buffImg, "PNG", new File(desPath));
    }

    /**
     * @param path      源图片文件夹路径
     * @param modPath   修改大小后图片文件夹路径
     * @param scaleSize 图片的修改比例，目标宽度
     */
    public static void getFiles(String path, String modPath, int scaleSize) throws IOException {
        ArrayList<String> files = new ArrayList<String>();
        File file = new File(path);
        File[] tempList = file.listFiles();
        //循环读取目录下图片
        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
                System.out.println("文件：" + tempList[i].getName() + "-" + tempList[i].getAbsolutePath().replaceAll("\\\\", "/"));
                String[] imagePath = tempList[i].getAbsolutePath().replaceAll("\\\\", "/").split("/");
                String imageNumber = null;
                ImageResizer.resizeImage(tempList[i].getAbsolutePath().replaceAll("\\\\", "/"), modPath+tempList[i].getName(), scaleSize);
                files.add(tempList[i].toString());
            }
            if (tempList[i].isDirectory()) {
//                  System.out.println("文件夹：" + tempList[i]);
            }
        }
        System.out.println(path + "下文件数量：" + files.size());
    }


    public static void main(String[] args) throws IOException {
        String dir = "G://pdf//江西广源化工//";
        String dir2 = "G://pdf//small//";
        long l = System.currentTimeMillis();
        getFiles(dir, dir2, 1000);
        System.out.println((System.currentTimeMillis()-l)/1000);


    }
}
